﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;

namespace Core
{
    public class ParamQuery
    {
        public ParamQuery() {
            _sbSelect = new StringBuilder();
            _sbFrom = new StringBuilder();
            _sbWhere = new StringBuilder(" 1 = 1 ");
            _sbSortName = new StringBuilder();
        }

        public static ParamQuery Instance() {
            return new ParamQuery();
        }

        public ParamQuery AddWhere(string paramName, string value) {
            _sbWhere.AppendFormat(" And {0} = '{1}'", FilterSql.FilterField(paramName), FilterSql.FilterValue(value));
            return this;
        }

        public ParamQuery AddWhere(string AndOrWhere) {
            _sbWhere.AppendFormat(" {0} ", AndOrWhere);
            return this;
        }

        private StringBuilder _sbWhere { get; set; }

        public string Where {
            get {
                var whereStr = _sbWhere.ToString();
                if (whereStr.Trim().StartsWith("and", StringComparison.InvariantCultureIgnoreCase) || whereStr.Trim().StartsWith("or", StringComparison.InvariantCultureIgnoreCase))
                    whereStr = " 1=1 " + whereStr;
                if (string.IsNullOrEmpty(whereStr))
                    whereStr = " 1=1 ";

                //whereStr += " And isnull(state,0) <> '2'";
                return whereStr;
            }
            set {
                _sbWhere.Clear();
                _sbWhere.Append(value);
            }
        }

        private StringBuilder _sbSelect { get; set; }
        public string Select {
            get { return _sbSelect.ToString(); }
            set {
                _sbSelect.Clear();
                _sbSelect.Append(value);
            }

        }

        private StringBuilder _sbFrom { get; set; }
        public string From {
            get { return _sbFrom.ToString(); }
            set {
                _sbFrom.Clear();
                _sbFrom.Append(value);
            }
        }

        private StringBuilder _sbSortName { get; set; }
        public string SortName {
            get { return _sbSortName.ToString(); }
            set {
                _sbSortName.Clear();
                _sbSortName.Append(value);
            }
        }

        public ParamQuery SetOrderBy(string orderBy) {
            OrderBy = orderBy;
            return this;
        }

        public string SortOrder { get; set; }

        private string _orderBy = null;

        public string OrderBy {
            get {
                if (string.IsNullOrEmpty(_orderBy)) _orderBy = string.Format(" {0} {1} ", SortName, SortOrder);
                return _orderBy;
            }
            set { _orderBy = value; }
        }

        private int _pageNumber;
        public int PageNumber {
            get {
                if (_pageNumber == 0)
                    _pageNumber = 1;
                return _pageNumber;
            }
            set { _pageNumber = value; }
        }

        private int _pageSize = 10;
        public int PageSize {
            get { return _pageSize; }
            set { _pageSize = value; }
        }

        public string GetSql() {
            StringBuilder sbSql = new StringBuilder();
            sbSql.AppendFormat("Select {0} From {1} ", Select, From);
            if (!string.IsNullOrEmpty(Where)) {
                sbSql.AppendFormat(" Where {0} ", Where);
            }
            if (!string.IsNullOrEmpty(SortName)) {
                sbSql.AppendFormat(" Order By {0} {1} ", SortName, SortOrder);
            }
            return sbSql.ToString();
        }

        public string TableName { get; set; }

        public string CustTableName { get; set; }

    }
}
